using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace ERPBackend.Migrations
{
    /// <inheritdoc />
    public partial class AddEBOMTables : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "e_b_o_m",
                columns: table => new
                {
                    id = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    bom_code = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false),
                    material_id = table.Column<int>(type: "int", nullable: false),
                    material_name = table.Column<string>(type: "nvarchar(255)", maxLength: 255, nullable: true),
                    material_specification = table.Column<string>(type: "nvarchar(255)", maxLength: 255, nullable: true),
                    version = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false),
                    status = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false),
                    description = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    approved_by = table.Column<int>(type: "int", nullable: true),
                    approved_time = table.Column<DateTime>(type: "datetime2", nullable: true),
                    remark = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    created_by = table.Column<int>(type: "int", nullable: false),
                    created_time = table.Column<DateTime>(type: "datetime2", nullable: false),
                    updated_by = table.Column<int>(type: "int", nullable: false),
                    updated_time = table.Column<DateTime>(type: "datetime2", nullable: false),
                    is_deleted = table.Column<bool>(type: "bit", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_e_b_o_m", x => x.id);
                    table.ForeignKey(
                        name: "FK_e_b_o_m_material_material_id",
                        column: x => x.material_id,
                        principalTable: "material",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "e_b_o_m_change",
                columns: table => new
                {
                    id = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    bom_id = table.Column<int>(type: "int", nullable: false),
                    old_version = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false),
                    new_version = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false),
                    change_type = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false),
                    change_reason = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    changed_by = table.Column<int>(type: "int", nullable: false),
                    changed_time = table.Column<DateTime>(type: "datetime2", nullable: false),
                    remark = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    created_by = table.Column<int>(type: "int", nullable: false),
                    created_time = table.Column<DateTime>(type: "datetime2", nullable: false),
                    updated_by = table.Column<int>(type: "int", nullable: false),
                    updated_time = table.Column<DateTime>(type: "datetime2", nullable: false),
                    is_deleted = table.Column<bool>(type: "bit", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_e_b_o_m_change", x => x.id);
                    table.ForeignKey(
                        name: "FK_e_b_o_m_change_e_b_o_m_bom_id",
                        column: x => x.bom_id,
                        principalTable: "e_b_o_m",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "e_b_o_m_item",
                columns: table => new
                {
                    id = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    bom_id = table.Column<int>(type: "int", nullable: false),
                    material_id = table.Column<int>(type: "int", nullable: false),
                    material_name = table.Column<string>(type: "nvarchar(255)", maxLength: 255, nullable: true),
                    specification = table.Column<string>(type: "nvarchar(255)", maxLength: 255, nullable: true),
                    unit = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: true),
                    quantity = table.Column<decimal>(type: "decimal(18,4)", nullable: false),
                    scrap_rate = table.Column<decimal>(type: "decimal(18,4)", nullable: false),
                    total_quantity = table.Column<decimal>(type: "decimal(18,4)", nullable: false),
                    position = table.Column<int>(type: "int", nullable: false),
                    remark = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    created_by = table.Column<int>(type: "int", nullable: false),
                    created_time = table.Column<DateTime>(type: "datetime2", nullable: false),
                    updated_by = table.Column<int>(type: "int", nullable: false),
                    updated_time = table.Column<DateTime>(type: "datetime2", nullable: false),
                    is_deleted = table.Column<bool>(type: "bit", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_e_b_o_m_item", x => x.id);
                    table.ForeignKey(
                        name: "FK_e_b_o_m_item_e_b_o_m_bom_id",
                        column: x => x.bom_id,
                        principalTable: "e_b_o_m",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_e_b_o_m_item_material_material_id",
                        column: x => x.material_id,
                        principalTable: "material",
                        principalColumn: "id");
                });

            migrationBuilder.CreateIndex(
                name: "IX_b_o_m_change_bom_id",
                table: "b_o_m_change",
                column: "bom_id");

            migrationBuilder.CreateIndex(
                name: "IX_b_o_m_item_bom_id",
                table: "e_b_o_m_item",
                column: "bom_id");

            migrationBuilder.CreateIndex(
                name: "IX_b_o_m_item_material_id",
                table: "e_b_o_m_item",
                column: "material_id");

            migrationBuilder.CreateIndex(
                name: "IX_e_b_o_m_bom_code_version",
                table: "e_b_o_m",
                columns: new[] { "bom_code", "version" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_e_b_o_m_material_id",
                table: "e_b_o_m",
                column: "material_id");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "b_o_m_change");

            migrationBuilder.DropTable(
                name: "b_o_m_item");

            migrationBuilder.DropTable(
                name: "e_b_o_m");
        }
    }
}
